//
//  MutableSelectViewController.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-4-18.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "MutableSelectViewController.h"
#import "NSString+Addtions.h"

@interface MutableSelectViewController ()

@end

@implementation MutableSelectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"附件";
    self.selectedIndexArray = [[NSMutableArray alloc] initWithCapacity:5];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [self addRightNavigationButton];
}

- (void)addRightNavigationButton {
    [self setRightBarButtonItemWithTitle:@"确定" action:@selector(saveAndBack)];
}

-(void)setRightBarButtonItemWithTitle:(NSString *)title action:(SEL)action
{
    if (ISIOS7) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    }
    else
    {
        CGFloat bw = 44;
        if (title.length>=4) {
            bw = 66;
        }
        UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, bw, 44)];
        [rightButton setTitle:title forState:UIControlStateNormal];
        [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [rightButton.titleLabel  setFont:[UIFont systemFontOfSize:16]];
        //        [rightButton setTitleColor:[self getThemeColor] forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    }
}

- (void)saveAndBack {
    [self.navigationController popViewControllerAnimated:YES];
    if (_delegate && [_delegate respondsToSelector:@selector(backToLastView:)]) {
        [_delegate backToLastView:_selectedIndexArray];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.datasource &&
       [self.datasource respondsToSelector:@selector(mutableSelectCtrl:numberOfRowsInSection:)])
    {
        return [self.datasource mutableSelectCtrl:self numberOfRowsInSection:section];
    }

    return 0;
}

- (int)getDataArrayCount {
    return [self tableView:_tableView numberOfRowsInSection:0];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.datasource &&
       [self.datasource respondsToSelector:@selector(mutableSelectCtrl:cellForIndexPath:)])
    {
        UITableViewCell *cell = [self.datasource mutableSelectCtrl:self cellForIndexPath:indexPath];
        if ([self isSelectedIndex:indexPath.row]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    return nil;
}

- (BOOL)isSelectedIndex:(int)index {
    if (_selectedIndexArray.count > 0) {
        if ([_selectedIndexArray containsObject:[NSString stringInt:index]]) {
            return YES;
        }
        return NO;
    }
    return NO;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.delegate &&
       [self.delegate respondsToSelector:@selector(mutableSelectCtrl:heightForRowAtIndexPath:)])
    {
        return [self.delegate mutableSelectCtrl:self heightForRowAtIndexPath:indexPath];
    }
    
    return 0.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [_selectedIndexArray addObject:[NSString stringInt:indexPath.row]];
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [_selectedIndexArray removeObject:[NSString stringInt:indexPath.row]];
    }
}

- (void)reloadTableView {
    [_tableView reloadData];
}

@end
